﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cstdx.Proto.Std;


	/// <summary>
	/// 财务信息请求包结构
	/// </summary>
	public class FinanceInfoRequest
	{
		/// <summary>
		/// 未知用途的14字节数据
		/// </summary>
		public byte[] Unknown1 { get; set; } = new byte[14];

		/// <summary>
		/// 市场类型
		/// </summary>
		public Exchange.MarketType Market { get; set; }

		/// <summary>
		/// 股票代码（6字节）
		/// </summary>
		public string Code { get; set; } = string.Empty;

		/// <summary>
		/// 请求包序列化
		/// </summary>
		/// <returns>序列化后的字节数组</returns>
		public byte[] Marshal()
		{
			using (var ms = new MemoryStream())
			using (var writer = new BinaryWriter(ms))
			{
				// 写入Unknown1（14字节）
				writer.Write(Unknown1, 0, Math.Min(Unknown1.Length, 14));
				// 不足14字节则补0
				if (Unknown1.Length < 14)
				{
					writer.Write(new byte[14 - Unknown1.Length], 0, 14 - Unknown1.Length);
				}

				// 写入市场类型（1字节，小端序）
				writer.Write((byte)Market);

				// 写入股票代码（6字节，小端序，使用ASCII编码）
				byte[] codeBytes = Encoding.ASCII.GetBytes(Code);
				byte[] fixedCode = new byte[6];
				Array.Copy(codeBytes, fixedCode, Math.Min(codeBytes.Length, 6));
				writer.Write(fixedCode);

				return ms.ToArray();
			}
		}
	}




